import getProp from './prop';
import { validateRequired } from '../rules/utils';
import { validateArrLen } from '../rules/array';
import { isNullPointer } from '../../utils/value';

const TIPS_FILE = '上传';
/**
 * 获取验证字段
 * @param {*} { fields }
 * @return {*} rule
 */
function _getRules({ fields }) {
  const { label, required } = getProp({ fields });
  // download 下载设置
  // safe 水印
  const { verification, download = 1, safe = false } = fields;
  // 下载
  const {
    type, min_count = 0, max_count = 0, size = 0, file_type
  } = verification;

  return {
    rules: [
      validateRequired(type, required, `请${TIPS_FILE}${label}！`),
      validateArrLen({ min_count, max_count }, TIPS_FILE)
    ],
    maxCount: max_count,
    minCount: min_count,
    size,
    download: 1,
    isWatermark: safe,
    fileType: file_type
  };
}

/**
 * @export 获取file类型表单属性
 * @param {*} { fields }
 * @return {*} prop
 */
export default function getFileProp({ fields }) {
  const rules = _getRules({ fields });

  return {
    ...getProp({ fields }),
    ...rules
  };
}
